import cv2 as cv
from alexnet import AlexNet

class Camera():
    def __init__(self):#初始化
        self.camera = cv.VideoCapture(0)
        self.camera.set(cv.CAP_PROP_FRAME_WIDTH, 1080)
        self.camera.set(cv.CAP_PROP_FRAME_HEIGHT, 720)

    def readCamera(self, paht):
        img, frameAll, contours = None, 0, []
        while self.camera.isOpened():
            ret, img = self.camera.read()
            frameAll += 1

            if (frameAll % 5 == 0):#更新轮廓
                imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
                edge = cv.Canny(img, 128, 200)
                contours, hierarchy = cv.findContours(edge, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)

            for contour in contours:
                x, y, w, h = cv.boundingRect(contour)
                if (w * h) > 20000:
                    imgTemp = img[y - 40: y + h + 40, x - 40: x + w + 40]
                    cv.rectangle(img, (x - 40, y - 40), (x + w + 40, y + h + 40), (0, 255, 0), 1)

            cv.imshow('frame', img)
            c = cv.waitKey(1)
            if c == 27:
                break
        camera.release()
        cv.destroyAllWindows()

if __name__ == "__main__":
    myVideo = Camera()
    myVideo.readCamera()






